Database.js ➔ constructor   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
const mysql = require('mysql')
2
3
class Database {
4
    constructor(config) {
5
        this.connection = mysql.createConnection(config);
6
    }
7
8
    query(sql, args) {
9
        return new Promise((resolve, reject) => {
10
            this.connection.query(sql, args, (error, rows) => {
11
                if (error) {
12
                    return reject(error)
13
                }
14
15
                return resolve(rows)
16
            })
17
        })
18
    }
19
20
    close() {
21
        return new Promise((resolve, reject) => {
22
            this.connection.end( error => {
23
                if (error) {
24
                    return reject(error)
25
                }
26
27
                return resolve()
28
            })
29
        })
30
    }
31
}
32
33
Database.execute = function (config, callback) {
34
    const database = new Database(config)
35
36
    return callback(database).then(
37
        result => database.close().then(() => result),
38
        error => database.close().then(() => { throw error })
39
    )
40
}
41
42
module.exports = {
43
    Database
44
}
45